package org.yyjkcache.common;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class HttpGetRequest {
	private HttpDataClient client = null;
	private HttpClient hClient = null;
	private static HttpGetRequest request = new HttpGetRequest();
	private static Logger logger;

	private HttpGetRequest() {
		logger = LogManager.getLogger(HttpGetRequest.class);
		logger.info("登录系统");
		HttpDataClient client = null;
		Properties properties = PropertiesHandler.getValueOnRoot("login.properties");
		try {
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			nvps.add(new BasicNameValuePair("username", properties.getProperty("username")));
			nvps.add(new BasicNameValuePair("password", properties.getProperty("password")));
			client = new HttpDataClient();
			client.login(nvps, properties.getProperty("url"));
			hClient = client.getClient();
			this.client = client;
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
		
	/**
	 * 获取JSONObject (fastjson)
	 * @param url  url
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public JSONObject getJsonObjectUTF(String url) throws ClientProtocolException, IOException {
		return JSONObject.parseObject(getRemoteJSON(url));
	}
	/**
	 * 获取JSONArray(fastjson)
	 * @param url
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public JSONArray getJsonArrayUTF(String url) throws ClientProtocolException, IOException {
		return JSONArray.parseArray(getRemoteJSON(url));
	}

	/**
	 * 获取远程JSON字符串
	 * 
	 * @param url
	 * @return
	 * @throws IOException
	 */
	public String getRemoteJSON(String url) throws IOException {
		RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(1000).setConnectTimeout(1000).build();
		HttpGet get = new HttpGet(url);
		get.setConfig(requestConfig);
		CloseableHttpResponse response = null;
		String json = null;
		try {
			response = (CloseableHttpResponse) this.hClient.execute(get);
			if (null == response)
				return "[]";
			HttpEntity entity = response.getEntity();
			if (response.getStatusLine().getStatusCode() == 200) {
				json = EntityUtils.toString(entity, "UTF-8").trim();
			} else {
				client.loginAgain();
				this.hClient = client.getClient();
				json = "[]";
			}
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		} finally {
			if (null != response) {
				response.close();
				response = null;
			}
		}
		return json;
	}

	public static HttpGetRequest reuest() {
		return request;
	}

}
